import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.RspInterface 1.0
import com.AlmListModel 1.0


//批量强平之当前强平客户;带checkbox的ArrayListMsg
Item {
    property var tablelistName
    //: "RiskCust2"
    property var roleNames

    signal gridWidthChange(int col)
    signal loadHeaderFinish
    //参数row为-1时代表全部
    signal checkRowChange(int row, bool bCheck)
    signal rowDoubleChange(int row)
    signal rightClick(int row, int col)

    RowLayout {
        anchors.fill: parent

        TableView {
            id: tableList
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.right: parent.right
            anchors.rightMargin: 5
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 3
            anchors.top: parent.top
            //            anchors.fill: parent
            //            anchors.leftMargin: 5
            //            anchors.rightMargin: 5
            //            backgroundVisible: false
            //            frameVisible: false
            model: checkListModel
            headerDelegate: headerDele //表头委托
            itemDelegate: Rectangle {
                //            color: "#6000FF00" //"green"
                color: styleData.selected ? "#FF3399FF" : "white"
                border.width: 1
                border.color: "gray"
                Text {
                    anchors.verticalCenter: parent !== null ? parent.verticalCenter : undefined
                    anchors.left: parent !== null ? parent.left : undefined
                    anchors.leftMargin: 3
                    anchors.rightMargin: 2
                    anchors.right: parent !== null ? parent.right : undefined
                    color: styleData.textColor
                    elide: styleData.elideMode
                    text: styleData.value !== undefined ? styleData.value : ""
                    horizontalAlignment: styleData.textAlignment
                }

                MouseArea {
                    anchors.fill: parent
                    acceptedButtons: Qt.RightButton
                    onClicked: {
                        if (mouse.button == Qt.RightButton) {
                            //                        leftClick(styleData.row);
                            rightClick(styleData.row, styleData.column)
                            console.log("ListFrame2 Cell-onClicked RightButton")
                        } else if (mouse.button == Qt.LeftButton) {
                            console.log("ListFrame2 Cell-onClicked LeftButton "
                                        + styleData.selected)
                            //                            leftClick(styleData.row)
                        }
                    }
                    //                    onDoubleClicked: {
                    //                        console.log("ListFrame2 Cell-onDoubleClicked")
                    //                        doubleClick(styleData.row)
                    //                    }

                    //                    onPressed: {
                    //                        console.log("ListFrame2 Cell-onClicked onPressed")
                    //                    }
                }
            }

            Component.onCompleted: {
                //            console.log("updateColumn2Table TableView\'s C++ type -", tableList)
                console.log("onCompleted load ListFrame -", tablelistName)
                updateColumn(tablelistName)
            }
            onDoubleClicked: {
                console.log("onDoubleClicked:")
                rowDoubleChange(row)
            }
            TableViewColumn {
                role: "CheckCol"
                width: 20
                movable: false
                delegate: Component {
                    Rectangle {
                        CheckBox {
                            id: ctrl
                            anchors.horizontalCenter: parent.horizontalCenter
                            checked: styleData.value
                        }

                        MouseArea {
                            //lqh 2018-8-1 这个MouseArea让全选按钮无法控制点击过的checkbox的bug消失了，神奇
                            //估计是屏蔽了CheckBox本身的响应，由外部控制是否check
                            anchors.fill: parent
                            onClicked: checkRowChange(styleData.row,
                                                      !ctrl.checked)
                        }
                    }
                }
            }
        }
    }

    property var columnCount: 0

    function updateColumn(listName) {
        console.log("load ListFrame -", listName)
        if (listName == "")
            return
        //将现在的每列全部删除
        for (var i = columnCount; i > 0; i--) {
            tableList.removeColumn(i)
        }

        var roleNames = colCfg.getRoleNames(listName)
        var widths = colCfg.getColumnWidths(listName)
        var titles = colCfg.getTitles(listName)
        var aligns = colCfg.getColumnAligns(listName)
        console.log("list type " + titles)
        var i, str, sAlign
        columnCount = roleNames.length

        var prefix = 'import QtQuick 2.0;import QtQuick.Controls 1.4;TableViewColumn {width: ' //创建TableViewColumn的代码

        //循环添加TableViewColumn
        for (i = 0; i < roleNames.length; i++) {
            sAlign = aligns[i] === 2 ? "Text.AlignHCenter" : (aligns[i] === 3 ? "Text.AlignRight" : "Text.AlignLeft")
            str = prefix + widths[i] + ";role:\"" + roleNames[i]
                    + "\";horizontalAlignment:" + sAlign + ";title:\"" + titles[i] + "\"}"
            tableList.addColumn(Qt.createQmlObject(str, tableList,
                                                   "dynamicSnippet1"))
        }

        loadHeaderFinish()
    }

    function setColumnWidth(role, width) {
        console.log("setColumnWidth " + role + " " + width)
        for (var i = 0; i < tableList.columnCount; ++i) {
            var column = tableList.getColumn(i)
            if (column.role === role)
                column.width = width
        }
    }

    //定义表头的委托
    Component {
        id: headerDele
        Rectangle {
            id: headerRect
            height: 30
            border.color: "lightgray"
            color: "black"

            Text {
                text: styleData.value
                anchors.centerIn: parent
                //                font.family: qsTr("微软雅黑")
                //                font.pixelSize: 12
                font.pixelSize: 14
                color: "white"
            }

            onWidthChanged: gridWidthChange(styleData.column)
        }
    }
}
